package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.tsugun.mediaplayer.R;
import com.tsugun.mediaplayer.model.entity.ArtistModel;
import com.tsugun.mediaplayer.view.loader.MediaImageLoad;

import java.util.List;

import static com.tsugun.mediaplayer.presenter.utils.SettingPreferenceHelper.*;

/**
 * 歌手列表项适配器
 * Created by shize on 2017/4/13.
 */

public class ArtistItemAdapter extends RecyclerView.Adapter<ArtistItemAdapter.RecyclerViewHolder> {
    // 显示的图标
    private static final int ADD_ICON_NOT = 0x10;
    private static final int ADD_ICON = 0x20;

    private List<ArtistModel> mData;
    private LayoutInflater mInflater;
    private List<ArtistModel> mFollowList;
    private OnArtistItemClickListener mOnArtistItemClickListener;

    public interface OnArtistItemClickListener {
        void onItemClick(View view, int position);

        void onAddClick(int position, View v);
    }

    public ArtistItemAdapter(Context context, List<ArtistModel> mData, List<ArtistModel> follows) {
        this.mData = mData;
        mInflater = LayoutInflater.from(context);
        mFollowList = follows;
    }

    public void setOnArtistItemClickListener(ArtistItemAdapter.OnArtistItemClickListener
                                                     onArtistItemClickListener) {
        mOnArtistItemClickListener = onArtistItemClickListener;
    }

    @Override
    public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RecyclerViewHolder(mInflater.inflate(R.layout.item_artist, parent, false));
    }

    @Override
    public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
        holder.mTvArtist.setText(mData.get(position).getName());
        holder.mIvIcon.setImageResource(R.drawable.ic_artist);
        MediaImageLoad.netArtistImageLoad(mData.get(position), holder.mIvIcon);
        holder.mIvAdd.setTag(ADD_ICON_NOT);
        if (mFollowList.contains(mData.get(position))) {
            holder.mIvAdd.setImageResource(R.drawable.ic_checked);
        } else {
            holder.mIvAdd.setImageResource(R.drawable.ic_add_grey);
        }
        if (mOnArtistItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnArtistItemClickListener.onItemClick(v, holder.getAdapterPosition());
                }
            });
            holder.mIvAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnArtistItemClickListener.onAddClick(holder.getAdapterPosition(), v);
                }
            });
        }

        setSettingTheme(holder);
    }

    // 设置主题
    private void setSettingTheme(RecyclerViewHolder holder) {
        holder.mTvArtist.setTextColor(INSTANCE.getTEXT_COLOR());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class RecyclerViewHolder extends RecyclerView.ViewHolder {

        RoundedImageView mIvIcon;
        TextView mTvArtist;
        ImageView mIvAdd;

        RecyclerViewHolder(View itemView) {
            super(itemView);
            mIvIcon = (RoundedImageView) itemView.findViewById(R.id.id_item_artist_icon);
            mTvArtist = (TextView) itemView.findViewById(R.id.id_item_artist_name);
            mIvAdd = (ImageView) itemView.findViewById(R.id.id_item_artist_add);
        }
    }
}
